package cn.kgc.mes.type.controller;

import java.util.HashMap;
import java.util.List;
import java.util.Map;
import jakarta.servlet.http.HttpServletResponse;

import cn.kgc.mes.type.service.IItemTypeService;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import cn.kgc.mes.common.annotation.Log;
import cn.kgc.mes.common.core.controller.BaseController;
import cn.kgc.mes.common.core.domain.AjaxResult;
import cn.kgc.mes.common.enums.BusinessType;
import cn.kgc.mes.type.domain.MdItem;
import cn.kgc.mes.type.service.IMdItemService;
import cn.kgc.mes.common.utils.poi.ExcelUtil;
import cn.kgc.mes.common.core.page.TableDataInfo;

/**
 * 物料产品管理Controller
 * 
 * @author ruoyi
 * @date 2025-09-10
 */
@RestController
@RequestMapping("/item/item")
public class MdItemController extends BaseController
{
    @Autowired
    private IMdItemService mdItemService;
    @Autowired private IItemTypeService itemTypeService;

    /**
     * 查询物料产品管理列表
     */
//    @PreAuthorize("@ss.hasPermi('item:item:list')")
    @GetMapping("/list")
    public TableDataInfo list(MdItem mdItem)
    {
        System.out.println("物料分类");
        System.out.println(mdItem.getItemTypeId());
        //查询本id下所有子节点的id
        List<Long> itemTypeChildren = itemTypeService.selectItemTypeChildren(mdItem.getItemTypeId());
        mdItem.setItemTypeChildren(itemTypeChildren);
        startPage();
        List<MdItem> list = mdItemService.selectMdItemList(mdItem);
        return getDataTable(list);
    }

    /**
     * 导出物料产品管理列表
     */
//    @PreAuthorize("@ss.hasPermi('item:item:export')")
    @Log(title = "物料产品管理", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, MdItem mdItem)
    {
        List<MdItem> list = mdItemService.selectMdItemList(mdItem);
        ExcelUtil<MdItem> util = new ExcelUtil<MdItem>(MdItem.class);
        util.exportExcel(response, list, "物料产品管理数据");
    }

    /**
     * 获取物料产品管理详细信息
     */
//    @PreAuthorize("@ss.hasPermi('item:item:query')")
    @GetMapping(value = "/{itemId}")
    public AjaxResult getInfo(@PathVariable("itemId") Long itemId)
    {
        return success(mdItemService.selectMdItemByItemId(itemId));
    }

    /**
     * 新增物料产品管理
     */
//    @PreAuthorize("@ss.hasPermi('item:item:add')")
    @Log(title = "物料产品管理", businessType = BusinessType.INSERT)
    @PostMapping
    public Object add(@RequestBody MdItem mdItem)
    {
        int i = mdItemService.insertMdItem(mdItem);
        Map<String,Object> map=new HashMap<>();
        if(i>0){
            map.put("code",200);
            map.put("msg","添加成功");
            Long materialId = mdItem.getItemId();
            map.put("data", materialId);
        }else{
            map.put("code",0);
            map.put("msg","添加失败");
        }
        return  map;
    }

    /**
     * 修改物料产品管理
     */
//    @PreAuthorize("@ss.hasPermi('item:item:edit')")
    @Log(title = "物料产品管理", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody MdItem mdItem)
    {
        return toAjax(mdItemService.updateMdItem(mdItem));
    }

    /**
     * 删除物料产品管理
     */
//    @PreAuthorize("@ss.hasPermi('item:item:remove')")
    @Log(title = "物料产品管理", businessType = BusinessType.DELETE)
	@DeleteMapping("/{itemIds}")
    public AjaxResult remove(@PathVariable Long[] itemIds)
    {
        return toAjax(mdItemService.deleteMdItemByItemIds(itemIds));
    }

    //修改物料分类是否启用
    @PostMapping("/updatenew")
    public Object updata1(@RequestBody MdItem mdItem){
        boolean result=mdItemService.updateItemTypeIsEnable1(mdItem);
        Map<String,Object> map=new HashMap<>();
        if(result){
            map.put("code",200);
            map.put("msg","修改成功");
        }else{
            map.put("code",0);
            map.put("msg","修改失败");
        }
        return  map;
    }

}
